import numpy as np
from sklearn.svm import SVC

def classify(codeFile, labelFile, kernel = 'linear', foldCount = 5):
	print('Running classification for ' + codeFile + ' with labels ' + labelFile)
	X = np.load(codeFile)
	X = X.reshape((X.shape[0], -1))

	y = np.load(labelFile)

	foldSize = int(float(X.shape[0]) / foldCount)

	accs = np.zeros((foldCount))
	avgAcc = 0.0

	wrong = np.zeros((foldCount, int(np.max(y)) + 1))

	for i in range(foldCount):
		testStart = i * foldSize
		testEnd = testStart + foldSize

		xTrain = np.concatenate((X[:testStart], X[testEnd:]))

		xTest = X[testStart:testEnd]

		yTrain = np.concatenate((y[:testStart], y[testEnd:]))

		yTest = y[testStart:testEnd]

		clf = SVC(kernel = kernel)

		clf.fit(xTrain, yTrain)

		prediction = clf.predict(xTest)

		acc = (np.count_nonzero(prediction == yTest) / float(yTest.shape[0])) * 100.0

		accs[i] = acc
		avgAcc += acc

		for a in range(yTest.shape[0]):
			if yTest[a] != prediction[a]:
				wrong[i, int(yTest[a])] += 1

	wrong = wrong / np.sum(wrong, axis = -1)[:, np.newaxis]

	avgAcc /= float(foldCount)
	wrong = np.mean(wrong, axis = 0)

	return avgAcc, accs, wrong
